/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package courseScheduler;

import java.sql.SQLException;

/**
 *
 * @author david
 */
public class StudentNSection {
    private int id;
    private int studentID;
    private int sectionID;

    private java.sql.Connection conn;

    private java.sql.PreparedStatement getSNSStatement;
    private java.sql.PreparedStatement setSNSStatement;
    private java.sql.PreparedStatement insertSNSStatement;

    StudentNSection(int newStudentID, int newSectionID) {
        studentID = newStudentID;
        sectionID = newSectionID;
    }

    StudentNSection(java.sql.Connection newConn,int newID) throws java.sql.SQLException {
        prepareGetStmnt();
        getSNSStatement.setInt(1, newID);
        java.sql.ResultSet rs = getSNSStatement.executeQuery();
        if (rs.first()){
            int count = 0;
            do {
                count ++;
            } while (rs.next());
            if (count < 2) {
                studentID = rs.getInt(1);
                sectionID = rs.getInt(2);
            }
            else {
                System.out.println("Multiple values found");
            }
        }
        else {
            javax.swing.JOptionPane.showMessageDialog(null, "No entries found");
        }
    }

    private void prepareGetStmnt() throws java.sql.SQLException{
        getSNSStatement = conn.prepareStatement("SELECT student_id,section_id FROM student_section WHERE id = ?;");
    }

    public void prepareSetStmnt() throws java.sql.SQLException {
        setSNSStatement = conn.prepareStatement("UPDATE student_section student_id = " + studentID + ",section_id = " + sectionID + " WHERE id = ?;");
    }

    public void prepareInsertStmnt() throws java.sql.SQLException {
        insertSNSStatement = conn.prepareStatement("INSERT INTO student_section (student_id,section_id) VALUES (" + studentID + "," + sectionID + ");");
    }

    public static void getID(java.sql.Connection newConn, int studentID, int sectionID) throws java.sql.SQLException{
        java.sql.Statement stmnt = newConn.createStatement();
        java.sql.ResultSet rs = stmnt.executeQuery("SELECT id FROM student_section WHERE student_id = " + studentID + " AND sectionID = " + sectionID + ";");
    }
}